package com.zhuangfei.timetable.model;


import com.zhuangfei.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.app.Context;

import java.util.*;

/**
 * 颜色池，管理课程项可挑选的颜色
 */
public class ScheduleColorPool {

    Context context;

    /**
     * 课程不在本周时的背景色
     */
    private RgbColor uselessColor;

    private Map<String, Integer> colorMap;

    /**
     * true：非本周课程使用colorMap渲染
     * false：非本周课程使用uselessColor渲染
     */
    private boolean ignoreUserlessColor = false;

    public ScheduleColorPool(Context context) {
        this.context = context;
        setUselessColor(new RgbColor(ResourceTable.Color_useless));
        colorMap = new HashMap<>();
        reset();
    }

    /**
     * 获取颜色的映射Map
     *
     * @return colorMap
     */
    public Map<String, Integer> getColorMap() {
        if (colorMap == null) {
            return new HashMap<>();
        } else {
            return colorMap;
        }
    }

    /**
     * 设置colorMap
     *
     * @param colorMap
     * @return ScheduleColorPool
     */
    public ScheduleColorPool setColorMap(Map<String, Integer> colorMap) {
        this.colorMap = colorMap;
        return this;
    }

    /**
     * 获取渲染时是否忽略非本周颜色
     *
     * @return boolean
     */
    public boolean isIgnoreUserlessColor() {
        return ignoreUserlessColor;
    }

    /**
     * 设置渲染时是否忽略非本周颜色
     * false：非本周课程使用uselessColor渲染
     * true：非本周课程使用colorMap渲染
     *
     * @param ignoreUserlessColor
     * @return ScheduleColorPool
     */
    public ScheduleColorPool setIgnoreUserlessColor(boolean ignoreUserlessColor) {
        this.ignoreUserlessColor = ignoreUserlessColor;
        return this;
    }

    /**
     * 使用集合维护颜色池
     */
    private List<RgbColor> colorPool;

    /**
     * 获取非本周课程颜色
     *
     * @return RgbColor
     */
    public RgbColor getUselessColor() {
        return uselessColor;
    }

    /**
     * 获取非本周课程颜色
     *
     * @return int
     */
    public RgbColor getUselessColorWithAlpha() {
        return uselessColor;
    }

    /**
     * 设置非本周课程颜色
     *
     * @param uselessColor 非本周课程的颜色
     * @return ScheduleColorPool
     */
    public ScheduleColorPool setUselessColor(RgbColor uselessColor) {
        this.uselessColor = uselessColor;
        return this;
    }

    /**
     * 得到颜色池的实例，即List集合
     *
     * @return colorPool
     */
    public List<RgbColor> getPoolInstance() {
        if (colorPool == null) {
            colorPool = new ArrayList<>();
        }
        return colorPool;
    }

    /**
     * 从颜色池中取指定透明度的颜色
     *
     * @param random
     * @return int
     */
    public RgbColor getColorAutoWithAlpha(int random) {
        if (random < 0) {
            return getColorAuto(-random);
        } else {
            return getColor(random % size());
        }
    }

    /**
     * 根据索引获取颜色，索引越界默认返回 Color.GRAY
     *
     * @param i 索引
     * @return int
     */
    public RgbColor getColor(int i) {
        if (i < 0 || i >= size()) {
            return new RgbColor(244, 248, 248);
        } else {
            return colorPool.get(i);
        }
    }

    /**
     * 使用模运算根据索引从颜色池中获取颜色,
     * 如果i<0，转换为正数,
     * 否则：重新计算索引j=i mod size
     *
     * @param i
     * @return int颜色
     */
    public RgbColor getColorAuto(int i) {
        if (i < 0) {
            return getColorAuto(-i);
        } else {
            return getColor(i % size());
        }
    }

    /**
     * 将指定集合中的颜色加入到颜色池中
     *
     * @param ownColorPool 集合
     * @return ScheduleColorPool
     */
    public ScheduleColorPool addAll(Collection<? extends RgbColor> ownColorPool) {
        getPoolInstance().addAll(ownColorPool);
        return this;
    }

    /**
     * 颜色池的大小
     *
     * @return int
     */
    public int size() {
        if (getPoolInstance() == null) {
            return 0;
        } else {
            return getPoolInstance().size();
        }
    }

    /**
     * 清空颜色池，清空默认颜色
     *
     * @return ScheduleColorPool
     */
    public ScheduleColorPool clear() {
        getPoolInstance().clear();
        return this;
    }

    /**
     * 重置，先在池子里添加一些默认的课程项颜色
     *
     * @return ScheduleColorPool
     */
    public ScheduleColorPool reset() {
        RgbColor[] colors = new RgbColor[]{
                new RgbColor(170, 163, 219), new RgbColor(134, 172, 233), new RgbColor(146, 210, 97), new RgbColor(128, 216, 163),
                new RgbColor(241, 198, 114), new RgbColor(253, 173, 139), new RgbColor(173, 190, 255), new RgbColor(148, 214, 250),
                new RgbColor(195, 181, 246), new RgbColor(153, 204, 255), new RgbColor(251, 166, 237), new RgbColor(238, 99, 99),
                new RgbColor(238, 180, 180), new RgbColor(210, 180, 140), new RgbColor(205, 155, 155), new RgbColor(95, 158, 160)
        };
        clear();
        for (int i = 0; i < colors.length; i++) {
            add(colors[i]);
        }
        return this;
    }

    /**
     * 在颜色池中添加一些自定义的颜色
     *
     * @param colorIds 多个颜色
     * @return ScheduleColorPool
     */
    public ScheduleColorPool add(RgbColor... colorIds) {
        if (colorIds != null) {
            for (int i = 0; i < colorIds.length; i++) {
                colorPool.add(colorIds[i]);
            }
        }
        return this;
    }
}
